﻿Imports Mystery
Imports System.Runtime.CompilerServices

Public Interface IButton

    Function getImage() As Image

    Function getText() As String

    Sub Press()

    Event Clicked()

End Interface

''' <summary>
''' create a window out of a control behide a button
''' </summary>
''' <typeparam name="Control"></typeparam>
''' <remarks></remarks>
Public MustInherit Class UserControlButton(Of Control As {UserControl, New})
    Implements IButton

    Public Event Clicked() Implements MysteryControls.IButton.Clicked

    Protected MustOverride Function getImagePath() As String
    Protected MustOverride Function getTitle() As String

    Public Function getImage() As System.Windows.Controls.Image Implements MysteryControls.IButton.getImage
        Dim path As String = getImagePath()
        If String.IsNullOrEmpty(path) Then Return Nothing
        Dim result As New Image()
        Dim bit As BitmapImage = New BitmapImage(New Uri(path, UriKind.RelativeOrAbsolute))
        result.Source = bit
        Return result
    End Function

    Public Function getText() As String Implements MysteryControls.IButton.getText
        Return getTitle()
    End Function

    Private Shared _control As New Control
    Private Shared new_window As Window

    Private Sub instanceWindow()
        new_window = _control.instanceWindow()

        AddHandler new_window.Closed, Sub()
                                          new_window.Content = Nothing
                                          new_window = Nothing
                                      End Sub

    End Sub

    Public Sub Press() Implements MysteryControls.IButton.Press
        If new_window Is Nothing Then instanceWindow()
        new_window.Show()
        new_window.Focus()
    End Sub

End Class

Public Interface IContentButton
    Inherits IButton

    Property content As IContent

End Interface


Public Module ButtonExntension
    <Extension()>
    Public Function getMenuItem(b As IButton) As MenuItem
        Dim result As New MenuItem
        Dim image As Image = b.getImage
        If image IsNot Nothing Then
            image.Width = 25
            image.Height = 25
            result.Icon = image
        End If
        
        result.Header = New Label With {.Content = b.getText}
        AddHandler result.Click, AddressOf b.Press
        Return result
    End Function
    <Extension()>
    Public Function getSquareButton(b As IButton, side As Integer) As Button
        Dim result As New Button
        Dim img As Image = b.getImage
        If img IsNot Nothing Then
            result.Content = img
        Else
            result.Content = New Label With {.Content = b.getText}
        End If
        result.Height = side
        result.Width = side
        AddHandler result.Click, AddressOf b.Press
        result.ToolTip = b.getText
        Return result

    End Function

    <Extension()>
    Public Function getStakPanel(icb As IContentButton, height As Integer) As StackPanel
        Dim myPanel As New StackPanel With {.Height = height, .Orientation = Orientation.Horizontal}
        Dim _content_button As New Button
        AddHandler _content_button.Click, AddressOf icb.Press
        _content_button.Content = icb.getText
        _content_button.MinWidth = 100
        _content_button.Height = height
        Dim image As Image = icb.getImage
        image.Height = height
        image.Width = height
        myPanel.Children.Add(image)
        myPanel.Children.Add(_content_button)

        Return myPanel

    End Function

End Module
